package ws.jdbc.concurrent.driver.sqlserver.cursor.wrapper;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Implementation of the {@link ICursorForwardReadOnly} services.
 * 
 * This cursor is type forward, so there's no need to {@link #restoreContext()}
 * on any of its operations.
 * 
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class CursorForwardReadOnly extends AbstractCursor implements ICursorForwardReadOnly {

  /**
   * Create new instance.
   *
   * @param rs {@link ResultSet} object that this cursor will work upon.
   */
  public CursorForwardReadOnly(ResultSet rs) {
    super(rs);
  }

  /**
   * {@inheritDoc}
   */
  public boolean next() throws SQLException {
    lock.lock();
    final boolean ret;
    try {
      currentCursor = id;
      ret = rs.next();
    } finally {
      lock.unlock();
    }
    return ret;
  }

  /**
   * {@inheritDoc}
   */
  public int getInt(int columnIndex) throws SQLException {
    lock.lock();
    final int ret;
    try {
      currentCursor = id;
      ret = rs.getInt(columnIndex);
    } finally {
      lock.unlock();
    }
    return ret;
  }

  /**
   * {@inheritDoc}
   */
  public String getString(int columnIndex) throws SQLException {
    lock.lock();
    final String ret;
    try {
      currentCursor = id;
      ret = rs.getString(columnIndex);
    } finally {
      lock.unlock();
    }
    return ret;
  }

  /**
   * {@inheritDoc}
   */
  public float getFloat(int columnIndex) throws SQLException {
    lock.lock();
    final float ret;
    try {
      currentCursor = id;
      ret = rs.getFloat(columnIndex);
    } finally {
      lock.unlock();
    }
    return ret;
  }
}
